package com.contexticon.Listeners;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import uiTest.PopUpMenuPanel;

import com.contexticon.Transcriber2.Transcriber5;


public class PopupListener implements MouseListener {
	
	JPopupMenu popupMenu = new JPopupMenu();


	public PopupListener() {

		JMenuItem menuItem = new JMenuItem("Merge rows");
		//menuItem.addActionListener(new ActionAdapter(this));
		popupMenu.add(menuItem);
		menuItem.addActionListener(new ActionAdapter());

		menuItem = new JMenuItem("Delete row");
		//menuItem.addActionListener(new ActionAdapter(this));
		popupMenu.add(menuItem);
		
		menuItem.addActionListener(new ActionAdapter());
	}
	
	public void mousePressed(MouseEvent e) {
	showPopup(e);
	}

	public void mouseReleased(MouseEvent e) {
		// if called by table
		if ((e.getComponent()) instanceof JTable) {
			
			//Transcriber5.table.getCellEditor().stopCellEditing();
			
			// which row the mouse click was on
			int row = Transcriber5.table.rowAtPoint(e.getPoint());
			// deselect all rows
			//Transcriber5.table.clearSelection();
			// select the row with the click
			//Transcriber5.table.addRowSelectionInterval(row,row);
		}
		
		showPopup(e);
	}

	private void showPopup(MouseEvent e) {
	if (e.isPopupTrigger()) {
		System.out.println();
	popupMenu.show(e.getComponent(), e.getX(), e.getY());
	}
	}

	public void mouseClicked(MouseEvent e) {
	}

	public void mouseEntered(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
	}
	} // end of popup listner





class ActionAdapter implements ActionListener {

	ActionAdapter() {
	}

	public void actionPerformed(ActionEvent e) {
		JMenuItem item = (JMenuItem) e.getSource();
		
		//Transcriber5.table.setEditingRow(-1);
		if (Transcriber5.table.isEditing()) {
			Transcriber5.table.getCellEditor().stopCellEditing();
		}
		
		if (item.getText() == "Merge rows") {	
			System.out.println("Merging rows");
			//adapter.insertColumn(e);

			DefaultTableModel dtm = (DefaultTableModel) Transcriber5.table.getModel();
			int[] rowsToDelete = Transcriber5.table.getSelectedRows();	
			
			if (rowsToDelete.length>1) {
				Float newStartTime = (Float) Transcriber5.table.getValueAt(rowsToDelete[0], 0);
				Float newEndTime = (Float) Transcriber5.table.getValueAt(rowsToDelete[rowsToDelete.length-1], 1);
			    String newUtterance="";
			    
				for (int i=0; i<rowsToDelete.length; i++) {					
					String ithUtterance = (String) Transcriber5.table.getValueAt(rowsToDelete[0], 2);
					newUtterance = newUtterance +ithUtterance;
					dtm.removeRow(rowsToDelete[0]);
				}
				
				dtm.insertRow(rowsToDelete[0], new Object[]{newStartTime, newEndTime, newUtterance});
			}
					
		} else if (item.getText() == "Delete row") {
			
	
			
			DefaultTableModel dtm = (DefaultTableModel) Transcriber5.table.getModel();
			int[] rowsToDelete = Transcriber5.table.getSelectedRows();			
			for (int i=0; i<rowsToDelete.length; i++) {
				dtm.removeRow(rowsToDelete[0]);
			}
			dtm.fireTableDataChanged();


			
			//adapter.deleteColumn(e);
		}

	}
}